using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputManager : SingletonpatternMonoBase<InputManager>
{
    public GridState currentGridState;

    public Vector3 nextSize = new Vector3(0, 0, 1);

    public Sprite[] sprites;
    public Sprite[] bigSprites;
    public Sprite[] soBigSprites;


    void Start()
    {

    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            HandleClick();
        }
    }
    void HandleClick()
    {
        Vector3 mousePos = Input.mousePosition;
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos) + MapManager.Instance.offset;//-1 1 
        worldPos.z = 0; // Ensure z-coordinate is 0                        //1  4
                        //5  7

        // Determine which big grid was clicked
        int bigRow = (int)(worldPos.x / MapManager.Instance.gridSize.x);
        int bigCol = (int)(worldPos.y / MapManager.Instance.gridSize.y);

        if (bigRow >= 0 && bigRow < 3 && bigCol >= 0 && bigCol < 3&&isAccordant(bigRow, bigCol))
        {
            BigGrid bigGrid = MapManager.Instance.bigGrids[bigRow, bigCol];
            Vector3 localPos = worldPos - bigGrid.transform.position;

            // Determine which small grid was clicked
            int smallRow = Mathf.FloorToInt(localPos.x);
            int smallCol = Mathf.FloorToInt(localPos.y);

            if (bigGrid.canDeploy&& smallRow >= 0 && smallRow < 3 && smallCol >= 0 && smallCol < 3)
            {
                SmallGrid chooseGrid = bigGrid.smallGrids[smallRow, smallCol];

                if (chooseGrid.gridState == GridState.Empty)
                {
                    chooseGrid.gridState = currentGridState;

                    if (MapManager.Instance.bigGrids[smallRow,smallCol ].canDeploy)
                    {
                        nextSize = new Vector3(smallRow,smallCol,  0);
                    }
                    else
                    {
                        nextSize = new Vector3(smallRow,smallCol,  1);
                    }
                    chooseGrid.GetComponent<SpriteRenderer>().sprite = sprites[(int)currentGridState];
                    bigGrid.CheckState();


                    CheckGridState();
                    UIManager.Instance.ChangeCurrentGridText(currentGridState);
                }
            }
        }
    }

    
    public void CheckGridState()
    {
        if(GameManager.Instance.gameState==1)
        {
            switch (currentGridState)
            {
                case GridState.X:
                    currentGridState = GridState.O;
                    break;
                case GridState.O:
                    currentGridState = GridState.X;
                    break;
            }
        }
        else if( GameManager.Instance.gameState==2)
        {
            switch (currentGridState)
            {
                case GridState.X:
                    currentGridState = GridState.O;
                    break;
                case GridState.O:
                    currentGridState = GridState.Y;
                    break;
                case GridState.Y:
                    currentGridState = GridState.X;
                    break;
            }
        }
        
    }

    public bool isAccordant(int x,int y)
    {
        if(nextSize.z==1) return true;
        return nextSize.x==x && nextSize.y==y;

    }

}
